/*
 * Copyright (C) 2014 VenomVendor <info@VenomVendor.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.naizhuai.ui.adapter.management;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.naizhuai.R;
import com.naizhuai.ui.entity.management.CompanyBean;
import com.naizhuai.ui.entity.management.OperateItemBean;
import com.naizhuai.ui.entity.management.DepartmentBean;
import com.naizhuai.util.ConstantsPersonResources;
import com.naizhuai.util.ImageUtils;
import com.naizhuai.util.SharedPreferenceUtil;

import java.util.List;

public class ManageOperateAdapterHolder extends RecyclerView.Adapter {

    private List<OperateItemBean> operateLists;
    private List<DepartmentBean> teamLists;
    private OnItemClickListener mItemClickListener;
    private Context mContext;

    public ManageOperateAdapterHolder(Context context,List<OperateItemBean> operateLists, List<DepartmentBean> teamLists) {
        this.mContext = context;
        this.operateLists=operateLists;
        this.teamLists = teamLists;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent , int viewType) {
        if(viewType==0) {
            View sView = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_company_show_card_top, parent, false);
            return new TopViewHolder(sView);
        }else if(viewType==1) {
            View sView = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_company_operate_card, parent, false);
            return new ContentViewHolder(sView);
        }else if(viewType==2){
            View sView = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_company_operate_card, parent, false);
            sView.setVisibility(View.GONE);
            ContentViewHolder content=new ContentViewHolder(sView);
            content.setTAG("NONE");
            return content;
        }else{
            View sView = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_team_content, parent, false);
            return new ContentTeamViewHolder(sView, viewType);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder , int position) {
        if(holder instanceof TopViewHolder){
            TopViewHolder topHolder= (TopViewHolder) holder;
            topHolder.vCOmName.setText(SharedPreferenceUtil.getInstance(mContext).getValue(CompanyBean.COMPANY_NAME_KEY,"暂时未加入公司"));
            ImageUtils.glideThumbViewByUrl(mContext,topHolder.mComLog,SharedPreferenceUtil.getInstance(mContext)
                    .getValue(CompanyBean.COMPANY_THUMB_URL_KEY, ConstantsPersonResources.ERROR_IMAGE_URL));
        }else if(holder instanceof ContentViewHolder) {
            ContentViewHolder contentHolder= (ContentViewHolder) holder;
            if(!contentHolder.getTAG().equals(""))
                return;
            contentHolder.vOperateName.setText(operateLists.get(position).getItemName());
            Glide.with(mContext).load(operateLists.get(position).getItemRes())
                    .placeholder(R.mipmap.ic_launcher).error(R.mipmap.ic_launcher)
                    .into( contentHolder.mOperateLog);
            if(operateLists.get(position).getItemDescribeStr()!=null&&!operateLists.get(position).getItemDescribeStr().equals(""))
                contentHolder.vDescribeTxt.setText(operateLists.get(position).getItemDescribeStr());
            else
                contentHolder.vDescribeTxt.setText("");
            if(operateLists.get(position).getItemDescribeRes()!=0){
                Drawable rightDrawable = mContext.getResources().getDrawable(operateLists.get(position).getItemDescribeRes());
                rightDrawable.setBounds(0, 0, rightDrawable.getMinimumWidth(), rightDrawable.getMinimumHeight());
                contentHolder.vDescribeTxt.setCompoundDrawables(null, null, rightDrawable, null);
            }else
                contentHolder.vDescribeTxt.setCompoundDrawables(null, null, null, null);
        }else if(holder instanceof ContentTeamViewHolder){
            ContentTeamViewHolder teamHolder= (ContentTeamViewHolder) holder;
            int tag = teamHolder.getTAG();
            position=position-operateLists.size();
            if (tag == 3) {
                teamHolder.fmLogoCard.setVisibility(View.GONE);
                teamHolder.tvTeamName.setText(mContext.getResources().getText(R.string.show_my_team));
                teamHolder.tvTeamName.getPaint().setFakeBoldText(true);
                teamHolder.tvTeamName.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
                teamHolder.tvTeamDescribe.setText("");
            } else {
                teamHolder.fmLogoCard.setVisibility(View.VISIBLE);
                teamHolder.tvTeamName.setText(teamLists.get(position).getDepartmentName());
                teamHolder.tvTeamDescribe.setText(String.format(mContext.getResources().getString(R.string.has_people), teamLists.get(position).getDepartmentMember().size()));
            }
        }
    }

    @Override
    public int getItemViewType(int position) {
        if(position==0)
            return 0;
        if(position<operateLists.size()){
            if(operateLists.get(position).getItemName()==null||operateLists.get(position).getItemName().equals(""))
                return 2;
            return 1;
        }
        if(position==operateLists.size())
            return 3;
        else
            return 4;
    }

    @Override
    public int getItemCount() {
        return operateLists.size()+teamLists.size();
    }

    public class TopViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        ImageView mComLog;
        TextView vCOmName;
        TextView invitePerson;

        public TopViewHolder(View view) {
            super(view);
            mComLog = (ImageView) view.findViewById(R.id.show_card_top_logo);
            vCOmName = (TextView) view.findViewById(R.id.show_card_top_title);
            invitePerson=(TextView) view.findViewById(R.id.add_new_person);
//            view.setOnClickListener(this);
            invitePerson.setOnClickListener(this);

        }


        @Override
        public void onClick(View v) {
            if (mItemClickListener != null) {
                mItemClickListener.onItemClick(v, getAdapterPosition(),-1);
            }
        }
    }


    public class ContentViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        ImageView mOperateLog;
        TextView vOperateName,vDescribeTxt;

        private String TAG="";

        public ContentViewHolder(View view) {
            super(view);
            mOperateLog = (ImageView) view.findViewById(R.id.operate_card_item_logo);
            vOperateName = (TextView) view.findViewById(R.id.show_card_item_name);
            vDescribeTxt= (TextView) view.findViewById(R.id.operate_item_details);

            view.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (mItemClickListener != null) {
                mItemClickListener.onItemClick(v, getAdapterPosition()-1,0);
            }
        }

        public String getTAG() {
            return TAG;
        }

        public void setTAG(String TAG) {
            this.TAG = TAG;
        }
    }


    public class ContentTeamViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        TextView tvTeamName, tvTeamDescribe;
        ImageView mvTeamLogo;
        ViewGroup fmLogoCard;

        private int TAG = -1;

        public ContentTeamViewHolder(View view, int viewType) {
            super(view);
            TAG = viewType;
            tvTeamName = (TextView) view.findViewById(R.id.team_name);
            tvTeamDescribe = (TextView) view.findViewById(R.id.team_describe);
            mvTeamLogo=(ImageView) view.findViewById(R.id.team_logo);
            fmLogoCard=(ViewGroup) view.findViewById(R.id.team_logo_card);
            if (viewType != 3)
                view.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (mItemClickListener != null) {
                mItemClickListener.onItemClick(v, getAdapterPosition() - 1-operateLists.size(),1);
            }
        }

        public int getTAG() {
            return TAG;
        }

        public void setTAG(int TAG) {
            this.TAG = TAG;
        }
    }

    public interface OnItemClickListener {
        public void onItemClick(View view, int position,int listIndex);
    }

    public void setOnItemClickListener(final OnItemClickListener mItemClickListener) {
        this.mItemClickListener = mItemClickListener;
    }

}
